<template></template>
  
  <script setup>
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElLoading } from 'element-plus'
import { token } from '@/api/auth'
import { feishuLogin } from '@/api/user' // 假设你有一个接口用来获取 token
import { userInfo } from '@/api/store'
import { Loading } from 'element-plus/es/components/loading/src/service.mjs'

const router = useRouter()

onMounted(async () => {
  const urlParams = new URLSearchParams(window.location.search)
  const code = urlParams.get('code')

  if (code) {
    try {
      // 向后端发送请求，带上 code 以获取 token
      const response = await feishuLogin({ code: code })
      const authToken = response.data.data.token

      if (authToken) {
        // 存储 token 到本地或状态管理中
        token.value = authToken
        userInfo.username = response.data.data.username
        userInfo.email = response.data.data.email
        userInfo.uuid = response.data.data.uuid
        userInfo.avatar = response.data.data.avatar
        ElMessage.success('登录成功')
        router.push('/') // 跳转到主页
      } else {
        throw new Error('未获取到 token')
      }
    } catch (error) {
      ElMessage.error('登录失败')
      router.push('/login') // 返回登录页
    }
  } else {
    ElMessage.error('授权失败或缺少授权码')
    router.push('/login') // 返回登录页
  }
})
</script>
  
